<script>
import { defineComponent, ref, watch, computed } from 'vue'
import FanOverlay from '../overlay/overlay.vue'
import FanButton from '../button/button.vue'

import { createNamespace, transformSize, transformStyleSize } from '../utils'

export default defineComponent({
  name: createNamespace('Popup')[0],
  components: { FanOverlay, FanButton },
  options: {
    // https://uniapp.dcloud.io/matter.html#mp
    virtualHost: true
  },
  props: {
    show: {
      type: Boolean,
      default: false,
      required: true
    },
    customStyle: [String, Object],
    position: {
      type: String,
      validator(value) {
        return ['center', 'top', 'bottom', 'right', 'left'].includes(value)
      },
      default: 'center'
    },
    /**
     * 圆角
     * @default 12px
     * @description true-默认值 false-0
     */
    round: {
      type: [Boolean, Number, String],
      default: '12px'
    },
    zIndex: {
      type: [String, Number],
      default: 1000
    },
    duration: {
      type: [String, Number],
      default: 0.3
    },
    title: String,
    titleAlign: {
      type: String,
      default: 'center',
      validator: (value) => ['left', 'center', 'right'].includes(value)
    },
    overlayStyle: {
      type: [Object, String]
    },
    closeOnClickOverlay: {
      type: Boolean,
      default: true
    },
    closeable: {
      type: Boolean,
      default: false
    },
    closeIcon: {
      type: String,
      default: 'close'
    },
    closeIconColor: {
      type: String,
      default: '#c8c9cc'
    },
    lockScroll: {
      type: Boolean,
      default: true
    },
    // 底部安全区域
    safeBottom: {
      type: Boolean,
      default: true
    }
  },
  emits: ['update:show', 'clickOverlay', 'closed'],
  setup(props, { emit }) {
    // overlay需要一个单独的控制，否则在 leave 时总是在内容动画执行完才执行
    const overlayShow = ref(props.show)
    const innerShow = ref(props.show)
    const time = ref(null)

    const className = ref('')
    // 显示动画
    const enter = () => {
      innerShow.value = true
      className.value = `fan-popup-${props.position}-enter-from`
      setTimeout(() => {
        className.value = ''
      }, 0)
    }
    // 隐藏动画
    // TODO 可能需要一个 innerShow 内部控制显、隐，否则异步关闭时没有动画（比如外部直接设置show=false关闭时）
    const leave = () => {
      className.value = `fan-popup-${props.position}-leave-active`
      time.value = setTimeout(() => {
        className.value = ''
        innerShow.value = false
        emit('update:show', false)
        emit('closed')
      }, props.duration * 1000)
    }
    watch(
      () => props.show,
      (val) => {
        if (className.value) return
        overlayShow.value = val
        clearTimeout(time.value)
        val ? enter() : leave()
      }
    )

    const theStyle = computed(() => {
      const { customStyle, round, zIndex, duration } = props
      const obj = {
        zIndex,
        '--fan-popup-time': `${duration}s`
      }
      if (round !== false && Number(round) !== 0) {
        obj['--fan-popup-radius'] = transformSize(round === true || !Number(round) ? '12px' : round)
      }
      return transformStyleSize(customStyle) + transformStyleSize(obj)
    })

    const overlayClick = () => {
      emit('clickOverlay')
      if (!props.closeOnClickOverlay) return
      emit('update:show', false)
    }

    return { overlayShow, innerShow, theStyle, className, leave, overlayClick }
  }
})
</script>

<template>
  <FanOverlay :style="overlayStyle" :show="overlayShow" :z-index="zIndex" :duration="duration" :lock-scroll="lockScroll"
    @click="overlayClick" />
  <div v-if="innerShow" :class="['fan-popup', 'fan-popup--' + position, { safe: safeBottom }, className]"
    :style="theStyle">
    <div v-if="title" :class="['fan-popup__title', 'fan-popup__title--' + titleAlign]">{{ title }}</div>
    <slot />
    <FanButton v-if="closeable" style="position:absolute;top:0;right:2px;" icon-size="16" padding="0" width="40px"
      :icon="closeIcon" :color="closeIconColor" :border="false" :plain="true" @click="leave" />
  </div>
</template>

<style lang="less">
.fan-popup {
  --fan-popup-radius: 0;
  --fan-popup-time: 0.3s;

  background-color: #fff;
  max-height: 80%;
  overflow-y: auto;
  position: fixed;
  transition: var(--fan-popup-time);

  &--center {
    top: 50%;
    left: 50%;
    transform: translate3d(-50%, -50%, 0);
    border-radius: var(--fan-popup-radius);
  }

  &--top {
    width: 100%;
    top: 0;
    left: 0;
    border-radius: 0 0 var(--fan-popup-radius) var(--fan-popup-radius);
  }

  &--right {
    top: 50%;
    right: 0;
    height: 100%;
    max-height: 100%;
    transform: translate3d(0, -50%, 0);
    border-radius: var(--fan-popup-radius) 0 0 var(--fan-popup-radius);
  }

  &--bottom {
    bottom: 0;
    left: 0;
    width: 100%;
    border-radius: var(--fan-popup-radius) var(--fan-popup-radius) 0 0;

    &.safe {
      padding-bottom: env(safe-area-inset-bottom);
    }
  }

  &--left {
    top: 50%;
    left: 0;
    height: 100%;
    max-height: 100%;
    transform: translate3d(0, -50%, 0);
    border-radius: 0 var(--fan-popup-radius) var(--fan-popup-radius) 0;
  }

  &-top-enter-from,
  &-right-enter-from,
  &-bottom-enter-from,
  &-left-enter-from {
    transition-timing-function: ease-out;
  }

  &-top-leave-active,
  &-right-leave-active,
  &-bottom-leave-active,
  &-left-leave-active {
    transition-timing-function: ease-in;
  }

  &-center-enter-from,
  &-center-leave-active {
    opacity: 0;
  }

  &-top-enter-from,
  &-top-leave-active {
    transform: translate3d(0, -100%, 0);
  }

  &-right-enter-from,
  &-right-leave-active {
    transform: translate3d(100%, -50%, 0);
  }

  &-bottom-enter-from,
  &-bottom-leave-active {
    transform: translate3d(0, 100%, 0);
  }

  &-left-enter-from,
  &-left-leave-active {
    transform: translate3d(-100%, -50%, 0);
  }

  &__title {
    font-size: 16px;
    color: var(--fan-black-9);
    font-weight: var(--fan-text-mbold);
    padding: 12px 15px;
    line-height: 1.4;

    &--center {
      text-align: center;
    }

    &--right {
      text-align: right;
    }
  }

  &__close-icon {
    position: absolute;
    z-index: 1;
    top: 10px;
    right: 16px;
    cursor: pointer;

    &:active {
      opacity: 0.7;
    }
  }
}
</style>